<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 17/4/25
 * Time: 下午1:17
 */

namespace app\models;

use app\librarys\DbCacheUtility;

/**
 * 系统菜单1
 * Class Menu
 * @package app\models
 */
class Menu
{
    const SessionKey = 'session_key_menu';

    /**
     * 获取系统菜单,按父子结构输出。只输出当前登录用户的有权限访问的菜单。
     * @return mixed
     */
    public static function getAllMenus()
    {
        if (\Yii::$app->session->has(self::SessionKey)) {
            return \Yii::$app->session->get(self::SessionKey);
        }

        $menuList = self::_getMenuList(0);
        foreach ($menuList as &$menu) {
            $subMenuList = self::_getMenuList($menu['id']);
            if (isset($subMenuList) && count($subMenuList) > 0) {
                $menu['sub_menu_list'] = $subMenuList;
            }
        }

        // 将菜单根据当前用户权限处理相应数据,将无权限的菜单移除
        $menuCount = count($menuList);
        for ($menuIndex = 0; $menuIndex < $menuCount; $menuIndex++) {
            $menu = &$menuList[$menuIndex];
            if (isset($menu['sub_menu_list'])) {
                $subMenuCount = count($menu['sub_menu_list']);
                for ($i = 0; $i < $subMenuCount; $i++) {
                    $subMenu = &$menu['sub_menu_list'][$i];
                    if (isset($subMenu['controller']) && isset($subMenu['action'])) {
                        if (!Permission::hasPermission($subMenu['controller'], $subMenu['action'])) {
                            unset($menu['sub_menu_list'][$i]);
                        }
                    }
                }
            }
            if (isset($menu['controller']) && isset($menu['action']) && $menu['controller'] != '' && $menu['action'] != '' && $menu['controller'] != null && $menu['action'] != null) {
                if (!Permission::hasPermission($menu['controller'], $menu['action'])) {
                    unset($menuList[$menuIndex]);
                }
            } else if (!(isset($menu['sub_menu_list']) && count($menu['sub_menu_list']) > 0)) {
                unset($menuList[$menuIndex]);
            }
        }


        \Yii::$app->session->remove(self::SessionKey);
        if (Manager::getCurrentManager() != null) {  // fixed by sky(如果session过期后,session中的manager为空,导致所有菜单都无权限访问。已知session过期后再登陆时菜单为空。)
            \Yii::$app->session->set(self::SessionKey, $menuList);
        }

        return $menuList;
//      return json_decode(' [{"id":"1","parent_id":"0","name":"\u4e3b\u9875","controller":"dashboard","action":"index","icon":null,"sort":"1"},{"id":"2","parent_id":"0","name":"\u5546\u54c1\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"2", "sub_menu_list":[{"id":"21","parent_id":"2","name":"\u8ba2\u5355\u7ba1\u7406","controller":"goods","action":"index","icon":null,"sort":"4"},{"id":"25","parent_id":"2","name":"\u4f1a\u5458\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"5"}]},{"id":"3","parent_id":"0","name":"\u673a\u6784\u7ba1\u7406","controller":"institution","action":"index","icon":"fa-book","sort":"3"},{"id":"4","parent_id":"0","name":"\u8ba2\u5355\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"4", "sub_menu_list":[{"id":"21","parent_id":"2","name":"订单列表","controller":"order","action":"index","icon":null,"sort":"4"},{"id":"25","parent_id":"2","name":"导入订单","controller":null,"action":null,"icon":null,"sort":"5"}]},{"id":"5","parent_id":"0","name":"\u4f1a\u5458\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"5"},{"id":"6","parent_id":"0","name":"\u53d1\u653e\u79ef\u5206","controller":null,"action":null,"icon":null,"sort":"6"},{"id":"7","parent_id":"0","name":"\u6d3b\u52a8\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"7"},{"id":"8","parent_id":"0","name":"\u4fe1\u606f\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"8"},{"id":"9","parent_id":"0","name":"\u8d22\u52a1\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"9"},{"id":"10","parent_id":"0","name":"\u62a5\u8868\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"10"},{"id":"11","parent_id":"0","name":"\u7cfb\u7edf\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"11"},{"id":"12","parent_id":"0","name":"\u65e5\u5fd7\u7ba1\u7406","controller":null,"action":null,"icon":null,"sort":"12"},{"id":"13","parent_id":"0","name":"\u5f00\u653e\u5e73\u53f0\u4fe1\u606f","controller":null,"action":null,"icon":null,"sort":"13"}]');
    }

    private static function _getMenuList($parentId)
    {
        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_menu WHERE parent_id=:parentId AND is_enabled=1 ORDER BY sort');
        $cmd->bindValue(':parentId', $parentId);
        return DbCacheUtility::queryAll($cmd, 0);
    }
}